﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class LogBO : Csla.BusinessBase<LogBO>
    {
        #region Business Properties

        private int? _LogID = null;
        public int? LogID
        {
            get { return this._LogID; }
            set { this._LogID = value; PropertyHasChanged("LogID"); }
        }

        private int? _NhanVienID = Session.LoginID;
        public int? NhanVienID
        {
            get { return this._NhanVienID; }
            set { this._NhanVienID = value; PropertyHasChanged("NhanVienID"); }
        }

        private string _PC = Session.LoginPC;
        public string PC
        {
            get { return this._PC; }
            set { this._PC = value; PropertyHasChanged("PC"); }
        }

        private string _TacVu = null;
        public string TacVu
        {
            get { return this._TacVu; }
            set { this._TacVu = value; PropertyHasChanged("TacVu"); }
        }

        private int? _ObjectID = null;
        public int? ObjectID
        {
            get { return this._ObjectID; }
            set { this._ObjectID = value; PropertyHasChanged("ObjectID"); }
        }

        private string _NewValue = null;
        public string NewValue
        {
            get { return this._NewValue; }
            set { this._NewValue = value; PropertyHasChanged("NewValue"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("DATC"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _LogID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newLogID = -1;
        private LogBO()
        {
            _LogID = newLogID--;
        }

        public LogBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public LogBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static LogBO NewRootLogBO()
        {
            return DataPortal.Create<LogBO>(new RootCriteria());
        }

        internal static LogBO NewChildLogBO()
        {
            return DataPortal.Create<LogBO>(new ChildCriteria());
        }

        public static LogBO GetRootLogBO(int _LogID)
        {
            return DataPortal.Fetch<LogBO>(new RootCriteria(_LogID));
        }

        public static LogBO GetRootLogBOByWhere(string where)
        {
            return DataPortal.Fetch<LogBO>(where);
        }

        internal static LogBO GetChildLogBO(SqlDataReader dataReader)
        {
            return new LogBO(dataReader);
        }

        public static void DeleteLogBO(int _LogID)
        {
            DataPortal.Delete(new RootCriteria(_LogID));
        }

        public static void DeleteLogBO(int _LogID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_LogID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int LogID;
            public bool directDelete = false;
            public RootCriteria(int _LogID)
            {
                this.LogID = _LogID;
                this.directDelete = false;
            }

            public RootCriteria(int _LogID, bool directDelete)
            {
                this.LogID = _LogID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int LogID;
            public bool directDelete = false;
            public ChildCriteria(int _LogID)
            {
                this.LogID = _LogID;
                this.directDelete = false;
            }

            public ChildCriteria(int _LogID, bool directDelete)
            {
                this.LogID = _LogID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("LogBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[LogID] = " + criteria.LogID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("LogBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["LogID"] is DBNull))
                this._LogID = (int)dataReader["LogID"];
            if (!(dataReader["NhanVienID"] is DBNull))
                this._NhanVienID = (int)dataReader["NhanVienID"];
            if (!(dataReader["PC"] is DBNull))
                this._PC = (string)dataReader["PC"];
            if (!(dataReader["TacVu"] is DBNull))
                this._TacVu = (string)dataReader["TacVu"];
            if (!(dataReader["ObjectID"] is DBNull))
                this._ObjectID = (int)dataReader["ObjectID"];
            if (!(dataReader["NewValue"] is DBNull))
                this._NewValue = (string)dataReader["NewValue"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            if (this._TacVu != null)
            {
                this._DATC = DateTime.MaxValue;

                SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
                SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("LogBO_InsertBO");
                db.AddInParameter(cmd, "@NhanVienID", SqlDbType.Int, this._NhanVienID);
                db.AddInParameter(cmd, "@PC", SqlDbType.NVarChar, this._PC);
                db.AddInParameter(cmd, "@TacVu", SqlDbType.NVarChar, this._TacVu);
                db.AddInParameter(cmd, "@ObjectID", SqlDbType.Int, this._ObjectID);
                db.AddInParameter(cmd, "@NewValue", SqlDbType.NText, this._NewValue);
                db.AddOutParameter(cmd, "@NewLogID", SqlDbType.Int, 10);
                db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
                db.ExecuteNonQuery(cmd);
                this._LogID = (int)cmd.Parameters["@NewLogID"].Value;
                this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
            }
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            if (this._TacVu != null)
            {
                SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
                SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("LogBO_UpdateBO");
                db.AddInParameter(cmd, "@LogID", SqlDbType.Int, this._LogID);
                db.AddInParameter(cmd, "@NhanVienID", SqlDbType.Int, this._NhanVienID);
                db.AddInParameter(cmd, "@PC", SqlDbType.NVarChar, this._PC);
                db.AddInParameter(cmd, "@TacVu", SqlDbType.NVarChar, this._TacVu);
                db.AddInParameter(cmd, "@ObjectID", SqlDbType.Int, this._ObjectID);
                db.AddInParameter(cmd, "@NewValue", SqlDbType.NText, this._NewValue);
                db.ExecuteNonQuery(cmd);
            }
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_LogID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("LogBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@LogID", SqlDbType.Int, criteria.LogID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_LogID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
